package com.nyist.zk.controller;

import com.nyist.zk.entities.Blood;
import com.nyist.zk.entities.BloodManager;
import com.nyist.zk.entities.UseBlood;
import com.nyist.zk.service.BloodManagerService;
import com.nyist.zk.service.BloodService;
import com.nyist.zk.service.UseBloodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class BloodManagerController {

    @Autowired
    private BloodManagerService bloodManagerService;

    @Autowired
    private UseBloodService useBloodService;

    @Autowired
    private BloodService bloodService;

    @PostMapping("/bloodManager/login")
    public String bloodManagerLogin(HttpSession session, BloodManager bloodManager, Map<String, Object> map) {
        if (!StringUtils.isEmpty(bloodManager.getBm_id())
                && !StringUtils.isEmpty(bloodManager.getBm_password())) {
            boolean flag = bloodManagerService.getBloodManagerByIdAndPassword(bloodManager.getBm_id(), bloodManager.getBm_password());
            if (flag) {
                //登录成功
                BloodManager byIdBloodManager = bloodManagerService.findByIdBloodManager(bloodManager.getBm_id());
                session.setAttribute("bloodManagerLoginUser", byIdBloodManager);
                System.out.println("登录成功!");
                return "bloodManager/bloodManagerIndex";
            }
        }
        //登录失败
        System.out.println("输入的id : " + bloodManager.getBm_id());
        System.out.println("输入的password : " + bloodManager.getBm_password());
        map.put("msg", "用户名或密码错误");
        //  redirect 重定向
        return "bloodManager/bloodManagerLogin";
    }

    @GetMapping("/bloodManager/{bm_id}")
    public String findByIdBloodManager(@PathVariable("bm_id") Integer bm_id, Map<String, Object> map) {
        System.out.println("--------查看血库管理员个人信息---------");
        BloodManager bloodManagerMap = bloodManagerService.findByIdBloodManager(bm_id);
        map.put("byIdBloodManagerMap", bloodManagerMap);
        return "bloodManager/bloodManagerList";
    }

    @GetMapping("/bloodManagerUpdates/{bm_id}")
    public String toUpdate(@PathVariable("bm_id") Integer bm_id, Map<String, Object> map) {
        System.out.println("--------准备修改血库管理员个人信息---------");
        BloodManager bloodManagerMap = bloodManagerService.findByIdBloodManager(bm_id);
        map.put("byIdBloodManagerMap", bloodManagerMap);
        return "bloodManager/bloodManagerUpdate";
    }

    @PutMapping("/bloodManagerUpdate")
    public String updateBloodManager(BloodManager bloodManager, Map<String, Object> map) {
        System.out.println("--------------修改血库管理员个人信息----------------");
        bloodManagerService.updateBloodManager(bloodManager);
        BloodManager bloodManagerMap = bloodManagerService.findByIdBloodManager(bloodManager.getBm_id());
        map.put("byIdBloodManagerMap", bloodManagerMap);
        return "bloodManager/bloodManagerList";
    }

    @GetMapping("/bloodManager/pwd")
    public String updateBloodManagerPassword() {
        System.out.println("--------------准备修改密码-----------------");
        return "bloodManager/bloodManagerPassword";
    }

    @ResponseBody
    @GetMapping("/bloodManager/pwd/{oldPwd}")
    public Boolean checkBloodManagerPwd(HttpSession session, @PathVariable("oldPwd") String oldPwd) {
        System.out.println("原来的密码:" + oldPwd);
        //1.从Session中获取当前登录用户的User对象
        BloodManager bloodManager = (BloodManager) session.getAttribute("bloodManagerLoginUser");
        if (bloodManager.getBm_password().equals(oldPwd)) {
            //输入的旧密码正确
            return true;
        }
        return false;
    }

    @PostMapping("/bloodManager/pwd")
    public String updateBloodManagerPwd(HttpSession session, String password) {
        //1.从Session中获取当前登录用户信息
        BloodManager bloodManager = (BloodManager) session.getAttribute("bloodManagerLoginUser");
        //2. 更新密码
        bloodManager.setBm_password(password);
        System.out.println("新的密码是:" + password);
        //3. 提交到数据库
        bloodManagerService.updateBloodManager(bloodManager);
        //4. 注销重新登录
        return "redirect:/blood";
    }

    /**************血库管理员对自己的操作结束******************/

    /**************血库管理员对血库的操作开始******************/

    @GetMapping("/bloods/{bm_id}")
    public String doctorFindAllBloods(@PathVariable("bm_id") Integer bm_id, Map<String, Object> map) {
        System.out.println("------------管理员编号为:" + bm_id + "查看血库开始--------------");
        List<Blood> bloodList = bloodService.findAllBlood();
        map.put("bloodList", bloodList);
        return "bloodManager/bloodsList";
    }

    @GetMapping("/bloodToNumUpdate/{b_type}")
    public String toBloodsUpdate(@PathVariable("b_type") String b_type, Map<String, Object> map) {
        System.out.println("--------准备修改血库---"+b_type+"类型的血液数量---------");
        Blood bloodMap = bloodService.findByTypeBlood(b_type);
        System.out.println("---------原来的数量为:  "+bloodMap.getB_num()+"----------------");
        map.put("bloodMap", bloodMap);
        return "bloodManager/bloodUpdate";
    }

    @PutMapping("/bloodsUpdate")
    public String updateBloods(Blood blood, Map<String, Object> map) {
        System.out.println("--------------修改血库容量信息----------------");
        System.out.println("---------修改后的数量为:  "+blood.getB_num()+"----------------");
        bloodService.updateBlood(blood);
        List<Blood> bloodList = bloodService.findAllBlood();
        map.put("bloodList", bloodList);
        return "bloodManager/bloodsList";
    }

    @GetMapping("/useBloodAllDoctor")
    public String doctorFindAllUseBloodsAllDoctor(Map<String, Object> map) {
        System.out.println("------------管理员查看血库申请开始--------------");
        List<UseBlood> doctorFindAllUseBlood = useBloodService.findAllUseBlood();
        map.put("doctorFindAllUseBlood", doctorFindAllUseBlood);
        return "bloodManager/bloodManagerFindAllUseBloodList";
    }

    @DeleteMapping("/bloodMangerDeleteNumBlood/{u_id}")
    public String bloodMangerDeleteNumBlood(Map<String, Object> map,@PathVariable("u_id")Integer u_id) {
        System.out.println("------------管理员放血开始--------------");
        UseBlood useBlood = useBloodService.findByUidUseBlood(u_id);
        Blood byTypeBlood = bloodService.findByTypeBlood(useBlood.getU_type());
        if (byTypeBlood.getB_num() >= useBlood.getU_num()) {
            int i = byTypeBlood.getB_num() - useBlood.getU_num();
            Blood blood = new Blood(byTypeBlood.getB_type(),i,byTypeBlood.getB_source(),byTypeBlood.getB_price());
            bloodService.updateBlood(blood);
            useBloodService.deleteUseBlood(u_id);
        }else {
            List<UseBlood> doctorFindAllUseBlood = useBloodService.findAllUseBlood();
            map.put("doctorFindAllUseBlood", doctorFindAllUseBlood);
            map.put("notNum", "没有足够的血液可以使用!!!!!!");
            return "bloodManager/bloodManagerFindAllUseBloodList";
        }
        return "bloodManager/bloodsList";
    }


}
